<!--  -->
<script setup>
import TabSwitcher from '@/components/TabSwitcher.vue'
import { useRoute } from 'vue-router'
import { ref, watch } from 'vue'
import { getUserInfoByNameAPI } from '@/apis/user'
import { useUserStore } from '@/stores'

const route = useRoute()
const initOrder = ref(null)
const userInfo = ref(null)
const userStore = useUserStore()
const isGuest = ref(null)

watch(() => route.params.id, () => init(), { immediate: true })

const tabs = ref([])

async function init() {
  let username = route.params.id
  initOrder.value = route.path.split('/').pop() || 'overview'

  const res = await getUserInfoByNameAPI(username).catch(() => null)
  if (res) userInfo.value = res.data
  isGuest.value = userStore.getUserInfo()?.user.username !== username
  tabs.value = isGuest.value
    ? [
      { title: '概览', path: `/users/${username}/overview`, id: 'overview' },
      { title: '回答', path: `/users/${username}/answers`, id: 'answers' },
      { title: '声望', path: `/users/${username}/reputation`, id: 'reputation' }
    ]
    : [
      { title: '概览', path: `/users/${username}/overview`, id: 'overview' },
      { title: '回答', path: `/users/${username}/answers`, id: 'answers' },
      { title: '收藏', path: `/users/${username}/bookmarks`, id: 'bookmarks' },
      { title: '声望', path: `/users/${username}/reputation`, id: 'reputation' },
      { title: '评论', path: `/users/${username}/comments`, id: 'comments' },
      { title: '投票', path: `/users/${username}/votes`, id: 'votes' },
    ]
}
</script>

<template>
  <div v-if="userInfo" class="profile-layout-container">
    <div class="profile">
      <div class="avatar-container">
        <div v-if="!userInfo?.user.avatar" class="avatar">
          {{ userInfo?.user.displayName[0].toUpperCase() }}
        </div>
        <img v-else class="avatar" src="https://random-cat-assets-r2.hats-land.com/api/v1/images" alt="avatar" />
      </div>
      <div class="info">
        <div class="username">{{ userInfo.user.displayName }}</div>
        <div class="id">@{{ userInfo.user.username }}</div>
        <span>{{ userInfo.user.ranks }} 声望</span>
        <span>{{ userInfo.user.answerCount }} 回答</span>
        <span>{{ userInfo.user.questionCount }} 问题</span>
      </div>
    </div>
    <TabSwitcher class="tab-switcher" :tabs="tabs" :active="initOrder" @on-switch-order="v => initOrder = v.id">
    </TabSwitcher>
    <RouterView :userInfo :isGuest></RouterView>
  </div>
</template>

<style scoped lang="scss">
.profile-layout-container {
  display: flex;
  flex-direction: column;
  height: 100%;
}

.profile {
  padding-bottom: 20px;
  display: flex;
  align-items: center;
  border-bottom: 1px solid #eee;

  .avatar-container {
    margin-right: 20px;
    width: 80px;
    height: 80px;

    .avatar {
      width: 100%;
      height: 100%;
      border-radius: 10%;
      object-fit: cover;
      border: 1px solid var(--border-secondary);
      color: rgb(0, 149, 99);
      background-color: #e2efd4;
      line-height: 80px;
      text-align: center;
      cursor: default;
      font-size: 40px;
      font-weight: bold;
    }
  }

  .info {
    margin-top: auto;

    .username {
      font-size: 20px;
      font-weight: bold;
    }

    .id {
      font-size: 14px;
      color: #999;
    }

    span {
      display: inline-block;
      margin-right: 10px;
      font-size: 14px;
      color: #999;
    }
  }
}

.tab-switcher {
  border-bottom: 1px dashed var(--border-secondary);
  padding-bottom: 1em;
  margin-bottom: 1em;
}
</style>
